package com.news.utils;

import com.news.common.CommonResult;
import com.news.common.ServletResp;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//@WebFilter("/*")
public class LoginFilter implements Filter {


	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest httpRequest = (HttpServletRequest)request;
		HttpServletResponse httpResponse = (HttpServletResponse)response;
		HttpSession session = httpRequest.getSession();
		String queryString = httpRequest.getQueryString();
if(true){
	chain.doFilter(request, response);
	return;
}



		// 放行路径
		ArrayList allowPathList = new  ArrayList();
		allowPathList.add("method=toLogin");
		allowPathList.add("method=doLogin");
		allowPathList.add("method=getArticleListByCatelogyId");
		allowPathList.add("/index.html");
		allowPathList.add("/admin");
		allowPathList.add("/article");
		allowPathList.add("/comment");
		allowPathList.add("/");
		allowPathList.add("/content.html");
		allowPathList.add("/newslist.html");
		allowPathList.add("/front/page/main.html");
		// 用户名
		String userName = (String) session.getAttribute("userName");

		String currentUrl = httpRequest.getServletPath();
		System.out.println("httpRequest.getServletPath() = " + currentUrl);
		System.out.println("httpRequest.getRequestURL() = " + httpRequest.getRequestURL());
		System.out.println("httpRequest.getRequestURI() = " + httpRequest.getRequestURI());
		// 如果是登录，要获取参数否则会死循环
		if ("method=doLogin".equals(queryString)){
			userName = request.getParameter("userName");
		}

		if (currentUrl.contains("css") || currentUrl.contains("js") || currentUrl.contains("images") || currentUrl.contains("iconfont")){
				chain.doFilter(request, response);
				return;
		}
		boolean noPass = false;
		// 不在放行路径
		if (!allowPathList.contains(currentUrl)){
			noPass = true;
		}

		// 如果用户名为null 并且 不在放行路径内
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {

	}
}
